﻿using ERPSystem.Common.Domains.Notices;
using ERPSystem.Common.DTO;
using ERPSystem.Common.Infrastructure.IRepository;
using ERPSystem.Enum.CommonErrorCodes;
using MediatR;
using Newtonsoft.Json;
using SqlSugar;

namespace SalesManagementCommon.API.SalesManagementCommons.BillingInvoicing
{
    public class BillingInvoiceBackFillHander : IRequestHandler<BillingInvoiceBackFillCommand, Result<PageResultDto<BillNoticesDTO>>>
    {
        private readonly ISqlSugarClient db;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="db"></param>

        public BillingInvoiceBackFillHander(ISqlSugarClient db)
        {
            this.db = db;
        }

        /// <summary>
        /// 开票通知单反填
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>

        public async Task<Result<PageResultDto<BillNoticesDTO>>> Handle(BillingInvoiceBackFillCommand request, CancellationToken cancellationToken)
        {
            var result = new Result<PageResultDto<BillNoticesDTO>>();
            var page = new PageResultDto<BillNoticesDTO>() { TotalCount = 0, TotalPage = 0, List = new List<BillNoticesDTO>() };
            try
            {
                RefAsync<int> totalCount = 0;
                RefAsync<int> totalPage = 0;
                var list = db.Queryable<Billingmaterial, Invoicingmemo>((b, bv) => b.ItemBianma == bv.ItemBianma).Select((b, bv) => new BillNoticesDTO()
                {
                    BillingmaterialId = b.BillingmaterialId,
                    SaleordersNumber = b.SaleordersNumber,
                    Documentencoding = b.Documentencoding,
                    ItemBianma = b.ItemBianma,
                    MaterialName = b.MaterialName,
                    Specifications = b.Specifications,
                    Measurenum = b.Measurenum,
                    BillingName = b.BillingName,
                    BillingNumber = b.BillingNumber,
                    BillingPrice = b.BillingPrice,
                    BillingSum = b.BillingSum,
                    Remark = b.Remark,
                    InvoicingmemoId = bv.InvoicingmemoId,
                    Documentsnumber = bv.Documentsnumber,
                    BusinessTypeId = bv.BusinessTypeId,
                    ClientName = bv.ClientName,
                    Bank = bv.Bank,
                    Deliverymailbox = bv.Deliverymailbox,
                    DocumentsDate = bv.DocumentsDate,
                    Operator = bv.Operator,
                    BlueandredId = bv.BlueandredId,
                    Account = bv.Account,
                    Documentstatus = bv.Documentstatus,
                    RateId = bv.RateId,
                    RateNumber = bv.RateNumber,
                    BusinessstateId = bv.BusinessstateId,
                    InvoiceTypeId = bv.InvoiceTypeId,
                    Addresstelephone = bv.Addresstelephone,
                    BeiZ = bv.BeiZ,
                    
                }).MergeTable();

                if (!string.IsNullOrEmpty(request.Documentsnumber)) //单据编码
                {
                    list = list.Where(s => s.Documentsnumber == request.Documentsnumber);
                }
                if (!string.IsNullOrEmpty(request.SaleordersNumber))// 销售订单号
                {
                    list = list.Where(b => b.SaleordersNumber.Contains(request.SaleordersNumber));
                }
                if (!string.IsNullOrEmpty(request.ClientName))// 客户名称
                {
                    list = list.Where(bv => bv.ClientName.Contains(request.ClientName));
                }
                if (!string.IsNullOrEmpty(request.BillingName))// 开票名称
                {
                    list = list.Where(b => b.BillingName.Contains(request.BillingName));
                }
                var res = await list.ToPageListAsync(request.PageIndex, request.PageSize, totalCount, totalPage);
                page.TotalCount = totalCount;
                page.TotalPage = totalPage;
                page.List = res;
                if (page.List.Any())
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Data = page;
                    result.Msg = "获取当前条件下开票通知单信息成功！";
                }

            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
